package com.demo.client;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * this class is to notify the message of user with NotififyManager
 * 
 * @author Administrator
 *
 */
public class Notifier {

	private static final String TAG = LogUtil.getClassName(Notifier.class);
	
	private static final Random random = new Random(System.currentTimeMillis());
	
	private SharedPreferences sharePre = null;
	private Context context;
	private NotificationManager notificationManager = null;
	
	public Notifier(Context context)
	{
		sharePre = context.getSharedPreferences(
				Constants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
		
		notificationManager = (NotificationManager)context.getSystemService(
				Context.NOTIFICATION_SERVICE);
		
		this.context = context;
	}
	
	public void notifyMessage(String id, String apiKey,
			String title, String message, String uri) {
		Log.d(TAG, "Notifier...start");
		
		Log.d(TAG, "Notification ..id:"+id);
		Log.d(TAG, "Notification ..apiKey:"+apiKey);
		Log.d(TAG, "Notification ..title:"+title);
		Log.d(TAG, "Notification ..message:"+message);
		Log.d(TAG, "Notification ..uri:"+uri);
	
		if(!isNotificationEnable())
			return;
		Notification notification = new Notification();
		notification.icon = getNotificationIcon();
		notification.defaults = Notification.DEFAULT_LIGHTS;
		
		//下面是使用十六的位状态来判断
		if(isNotificationSoundEnable())
		{
			notification.defaults |= Notification.DEFAULT_SOUND; //0x000001
		}
		if(isNotificationVibrateEnable())
		{
			notification.defaults |=Notification.DEFAULT_VIBRATE; //0x000002
		}
		notification.defaults |= Notification.FLAG_AUTO_CANCEL;
		notification.when = System.currentTimeMillis();
		notification.tickerText = message;
		
		Intent i = new Intent();
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		i.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
		i.putExtra(Constants.NOTIFICATION_ID, id);
		i.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
		i.putExtra(Constants.NOTIFICATION_TITLE, title);
		i.putExtra(Constants.NOTIFICATION_MESSAGE, message);
		i.putExtra(Constants.NOTIFICATION_URI, uri);
		
		PendingIntent pendingIntent = 
			PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
		
		notification.setLatestEventInfo(context, title, message, pendingIntent);
		notificationManager.notify(random.nextInt(), notification);
	}
	
	private int getNotificationIcon()
	{
		return sharePre.getInt(Constants.NOTIFICATION_ICON, 0);
	}
	private boolean isNotificationEnable()
	{
		return sharePre.getBoolean(Constants.SET_NOTIFICATION_ENABLE, false);
	}
	private boolean isNotificationSoundEnable()
	{
		return sharePre.getBoolean(Constants.SET_SOUND_ENABLE, false);
	}
	/**
	 * 配置文件里震动可以用否
	 * 
	 * @return
	 */
	private boolean isNotificationVibrateEnable()
	{
		return sharePre.getBoolean(Constants.SET_Vibrate_ENABLE, false);
	}
}
